package com.sgb.kjwl.view.ui.activity.self.packers;


import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtSelfEmploymentApplyBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.SelfEmploymentApplyEntity;
import com.sgb.kjwl.view.ui.adapter.self.packers.SelfEmploymentApplyAdapter;
import com.sgb.link.view.widget.XLinearLayoutManager;
import com.sgb.kjwl.viewmodel.self.packers.SelfEmploymentApplyModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

import javax.inject.Inject;

/**
 * description:用户中心-用工报名
 *
 * @author LiWei
 * @date 2020-11-30
 */
public class SelfEmploymentApplyActivity extends AppActivity {

    private AtSelfEmploymentApplyBinding binding;

    @Inject
    SelfEmploymentApplyModel viewModel;

    private SelfEmploymentApplyAdapter adapter;
    private static final int REQUEST_CODE = 10023;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SelfEmploymentApplyActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).
                selfModule(new SelfModule(this)).
                build().
                inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_self_employment_apply);
        binding.setViewModel(viewModel);

        if (adapter == null) {
            adapter = new SelfEmploymentApplyAdapter(this, null);
        }
        adapter.setOnItemClick(new AdapterOnItemClick<SelfEmploymentApplyEntity>() {
            @Override
            public void onItemClick(SelfEmploymentApplyEntity selfEmploymentApplyEntity, int position) {
                viewModel.getH5Url(selfEmploymentApplyEntity.getRelationNo());
            }
        });
        adapter.setListener(new SelfEmploymentApplyAdapter.ItemOnClickListener() {
            @Override
            public void delete(String no) {
                optState(no);
            }

            @Override
            public void edit(String no) {
                SelfEmploymentApplyEditActivity.start(SelfEmploymentApplyActivity.this, no, REQUEST_CODE);
            }
        });

        binding.recyclerView.setLayoutManager(new XLinearLayoutManager(this));
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(
                0,
                0,
                0,
                getResources().getDimension(R.dimen.dp5)));
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getEmploymentApplyListModel(false);
            }

            @Override
            public void onLoadMore() {
                viewModel.getEmploymentApplyListModel(true);
            }
        });
    }

    @Override
    protected void init() {
        viewModel.getEmploymentApplyListModel(false);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        }
    }

    public void showList(List<SelfEmploymentApplyEntity> list, boolean isLoadMore, boolean isLast) {
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list != null && list.size() > 0) {
                adapter.addDatas(list);
            }
            binding.recyclerView.setNoMore(isLast);
        } else {
            binding.recyclerView.refreshComplete();
            if (list != null && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
            } else {
                adapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                viewModel.getEmploymentApplyListModel(false);
            }
        }
    }

    private void optState(String no) {
        new CommonDialog(getActivity()).builder().setPositiveButton("确定").setNegativeButton("取消")
                .setTitle("确认删除？")
                .setDialogCallBack(new CommonDialog.DialogCallBack() {
                    @Override
                    public void onRightClick() {
                        viewModel.deleteEmploymentApply(no);
                    }

                    @Override
                    public void onLeftClick() {

                    }
                }).show();
    }
}
